package com.hsogoo.ultraman.spi;

import java.util.List;

import com.google.common.collect.Lists;
import com.hsogoo.ultraman.Config;
import com.hsogoo.ultraman.event.ConfigChangeEvent;
import com.hsogoo.ultraman.listener.ConfigChangeListener;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by za-huangsaigang on 2018/9/13.
 */
@Slf4j
public abstract class AbstractConfig implements Config {

    private List<ConfigChangeListener> listeners = Lists.newCopyOnWriteArrayList();

    @Override
    public void addChangeListener(ConfigChangeListener listener) {
        if (!listeners.contains(listener)) {
            listeners.add(listener);
        }
    }

    protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
        for (final ConfigChangeListener listener : listeners) {
            listener.onChange(changeEvent);
        }
    }

}
